/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.struts.actions;

import com.myapp.struts.dao.AuthenticationDao;
import java.sql.Connection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

/**
 *
 * @author pavan
 */
public class Authentication extends Action {

    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
    private final static String FAIL = "fail";
    private AuthenticationDao adao;
    private String role;
    private String[] name;
    private HttpSession session;

    public Authentication() {
        adao = new AuthenticationDao();
    }

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        session = request.getSession(true);
        /**Check username and password on database*/
        role = adao.check(form);
        name = role.split(",");

        if (role != null) {
            session.setAttribute("role", name[0]);
            session.setAttribute("name", name[1]);
            return mapping.findForward(SUCCESS);
        } else {
            return mapping.findForward(FAIL);
        }
    }
}
